import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Observable } from "rxjs/Observable";
import { DatePipe } from "@angular/common";
import { AppConfig } from "../config";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class DynamicReportService {
  /**
     * 时间格式化字符串
     * 
     * @private
     * @type {string}
     * @memberof HistoryService
     */
  private fomat: string = "yyyy-MM-dd HH:mm:ss";
  private readonly param = "valueHistory";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService, private datePipe: DatePipe) { }



  // getDynamicReportByfarmID(farmId: number, points: any[], starttime: Date, endtime: Date): Promise<any[]> {
  //   let startTime: string = "";
  //   let endTime: string = "";
  //   startTime = this.datePipe.transform(starttime, this.fomat);
  //   endTime = this.datePipe.transform(endtime, this.fomat);
  //   return this.http
  //     .post(this.url+'/farmByType', JSON.stringify({ farmId, points, startTime, endTime }), { headers: this.headers })
  //     .toPromise()
  //     .then(res => res.json().data)
  //     .catch(this.handleError);
  // }

  getDynamicReportByfarmID(farmIds: number[], points: any[], starttime: Date, endtime: Date): Promise<any[]> {
    let startTime: string = "";
    let endTime: string = "";
    startTime = this.datePipe.transform(starttime, this.fomat);
    endTime = this.datePipe.transform(endtime, this.fomat);
    // const url = `http://192.168.15.150:8585/webapi/v1/valueHistory`;
    return this.http
      .post(this.url + '/farmByType', JSON.stringify({ farmIds, points, startTime, endTime }))
      .toPromise()
      .then(res => {
        if (res.json().code === '200') {
          return res.json().data;
        } else {
          return [];
        }
      })
      .catch(this.handleError);
  }



  getDynamicReportByfarmIDs(farmIds: number[], points: any[], starttime: Date, endtime: Date): Promise<any[]> {
    let startTime: string = "";
    let endTime: string = "";
    startTime = this.datePipe.transform(starttime, this.fomat);
    endTime = this.datePipe.transform(endtime, this.fomat);
    // const url = `http://192.168.15.150:8585/webapi/v1/valueHistory`;
    return this.http
      .post(this.url + '/companyByType', JSON.stringify({ farmIds, points, startTime, endTime }))
      .toPromise()
      .then(res => {
        if (res.json().code === '200') {
          return res.json().data;
        } else {
          return [];
        }

      })
      .catch(this.handleError);
  }



  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }
}



// WEBPACK FOOTER //
// ./src/app/service/dynamic-report.service.ts